package com.fwj.threadexample;

/**
 * @author stone.feng
 * @since 2019-03-02
 */
public class MyThread12 extends Thread {
    private static int ticket = 1000;
    private static Object obj = new Object();

    public MyThread12(String name) {
        super(name);
    }

    @Override
    public void run() {

        while (true) {
            // 锁对象，要确保是同一把锁（同一个对象）
            synchronized (obj) {
                if (ticket > 0) {
                    System.out.println(super.getName() + "正在销售第" + ticket + "张票");
                    ticket--;
                } else {
                    System.out.println(super.getName() + "票已经销售完毕");
                    break;
                }
            }
        }
    }

    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            if (i <= 9) {
                new MyThread12("窗口00" + i).start();
            } else {
                new MyThread12("窗口010").start();
            }
        }
    }

}